from django.shortcuts import render
from .models import *
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import *
from .ser import *
# Create your views here.

# A4作业
class Moviemanage(APIView):
    def post(self,request):
        ser = Movieser(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code':200})
        else:
            print(ser.errors)
            return Response({'code':20010})
    def get(self,request):
        se = Movie.objects.all()
        ser = Movieser(se,many=True)
        return Response({'code':200,'list':ser.data})

class Actormanage(APIView):
    def post(self,request):
        ser = Actorser(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code':200})
        else:
            print(ser.errors)
            return Response({'code':20010})
    def get(self,request):
        se = Actor.objects.all()
        ser = Actorser(se,many=True)
        return Response({'code':200,'list':ser.data})


# A2作业
class Teachermanage(APIView):
    def post(self,request):
        ser = Teacherser(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code': 200})
        else:
            print(ser.errors)
            return Response({'code': 20010})


class TeacherM(APIView):
    def post(self,request):
        username = request.data.get('username')
        password = request.data.get('password')
        use = Teacher.objects.filter(username=username).first()
        if username==use.username:
            if password == use.password:
                return Response({'code':200,'userid':use.id,'username':use.username })
            else:
                return Response({'code':20010,'meg':'密码错误'})
        else:
            return Response({'code':20011,'meg':'用户名不存在'})

class Studentmanage(APIView):
    def post(self,request):
        ser = Studentser(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code': 200})
        else:
            print(ser.errors)
            return Response({'code': 20010})

    def get(self,request):
        userid = request.GET.get('userid')
        if userid:
            stu = Student.objects.filter(teacher_id=userid).all()
        else:
            stu = Student.objects.filter(teacher_id__isnull=True).all()
        ser = Studentser(stu,many=True)
        return Response({'code':200,'list':ser.data})

    def put(self,requset):
        id = requset.GET.get('id')
        userid = requset.GET.get('userid')
        if userid:
            # 绑定
            Student.objects.filter(id=id).update(teacher_id=userid)
        else:
            #解绑
            Student.objects.filter(id=id).update(teacher_id='')
        return Response({'code':200})


# A3

class Stureg(APIView):
    def post(self,request):
        ser = Stuser(data=request.data)
        if ser.is_valid():
            ser.save()
            stu = Stu.objects.filter(username=request.data.get('username')).first()
            return Response({'code':200,'id':stu.id,'name':stu.name})
        else:
            print(ser.errors)
            return Response({'code':20010})

    def get(self,request):
        id = request.GET.get('id')
        stu = Stu.objects.filter(id=id).first()
        ser = ShowStuser(stu)
        return Response({'code':200,'list':ser.data})


# 课程
class ShowCourse(APIView):
    def get(self,request):
        cla = Sclass.objects.all()
        ser = Sclassser(cla,many=True)
        return Response({'code':200,'list':ser.data})